<div class="widget">
	<div class="widget-header bg-blue">
		<i class="widget-icon icon-notebook"></i> 
		<span class="widget-caption"> 退费及终止 </span>
		<div class="widget-buttons">
			<a href="javascript:void(0);" onclick="handlerCancel(this);" title="关闭"
				 data-toggle="dispose" data-refresh-table="#table-contract"> <i class="fa fa-times"></i> </a> 
		</div>
	</div>
	<div class="widget-body">
		<!-- TODO 退费信息 -->
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-header bg-gold">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 处理信息 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-refund">
				<form class="form-horizontal form-editor form-editor-refund">
					
					<div class="row">
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="buying">购买课时</label>
								<div class="col-sm-8">
									<input type="text" id="buying" class="form-control" readonly="readonly" /> 
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="useing">已用课时</label>
								<div class="col-sm-8">
									<input type="text" id="useing" class="form-control" readonly="readonly" /> 
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="rested">剩余课时</label>
								<div class="col-sm-8">
									<input type="text" id="rested" class="form-control" readonly="readonly" /> 
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="totalsPrice">总金额</label>
								<div class="col-sm-8">
									<input type="text" id="totalsPrice" class="form-control" readonly="readonly" /> 
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="useingPrice">已用金额</label>
								<div class="col-sm-8">
									<input type="text" id="useingPrice" class="form-control" readonly="readonly" /> 
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="restedPrice">剩余金额</label>
								<div class="col-sm-8">
									<input type="text" id="restedPrice" class="form-control" readonly="readonly" /> 
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label">处理类型</label>
								<div class="col-sm-8">
									<div class="input-group">
										<label class="radio-inline c-radio"> 
											<input type="radio" name="type" value="终止" checked="checked"/> 
											<span class="fa fa-check"></span> 终止&emsp;
										</label> 
										<label class="radio-inline c-radio"> 
											<input type="radio" name="type" value="退费" /> 
											<span class="fa fa-check"></span> 退费
										</label>
									</div>
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="createDate">处理时间</label>
								<div class="col-sm-8">
									<div class="input-group date date-selector">
										<input name="createDate" id="createDate" type="text" class="form-control"> 
										<span class="input-group-addon">
											<span class="fa fa-calendar"></span>
										</span>
									</div> 
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="username">处理人</label>
								<div class="col-sm-8">
									<input type="text" name="username" id="username" class="form-control" readonly="readonly" /> 
									<input type="hidden" name="userid" id="userid" data-default-user/>
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="reason">终止原因</label>
								<div class="col-sm-8">
									<select name="reason" id="reason" class="chosen-select" data-url="/miwawa/default/common/select.json" data-pcode="c_reason_type" data-entity="com.app.miwawa.api.system.entity.config.Codec">
									</select>
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="amount">退费金额</label>
								<div class="col-sm-8">
									<div class="input-group">
										<input type="text" name="amount" id="amount" class="form-control" />
										<span class="input-group-addon">元</span>
									</div> 
								</div>
							</div>
						</div>
						<div class="col-sm-4">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="payment">退费方式</label>
								<div class="col-sm-8">
									<select name="payment" id="payment" class="chosen-select" data-url="/miwawa/default/common/select.json" data-pcode="c_payment_type" data-entity="com.app.miwawa.api.system.entity.config.Codec">
									</select>
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-sm-8">
							<div class="form-group">
								<label class="col-sm-2 control-label" for="remark">备注</label>
								<div class="col-sm-10">
									<input type="text" name="remark" id="备注" class="form-control"/> 
								</div>
							</div>
						</div>
					</div>
				</form>
				
				<div class="widget-footer editor-footer-sm" id="editor-tools-refund">
					<button type="button" class="btn btn-danger btn-sm" data-toggle="tooltip" data-placement="top" title="取消" onclick="handlerCancel(this);" data-refresh-table="#table-contract">
						<i class="glyphicon glyphicon-remove"></i>
					</button>
					<button type="button" class="btn btn-primary btn-sm" data-toggle="tooltip" data-placement="top" title="保存" 
									onclick="handlerSubmit(this, '#editor-form-refund', '.form-editor-refund', 'handlerBefore');" data-submit-callback="handlerCallback">
						<i class="glyphicon glyphicon-ok"></i>
					</button>
				</div>
			</div>
		</div>
		
		<!-- TODO 基本信息 -->
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 基本信息 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-contract">
				
			</div>
		</div>
		
		<!-- TODO 购买信息 -->
		<div class="widget radius-bordered mgbt-xs-10" id="editor-panel-buyinfo">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> <a href="" data-toggle="collapse"> 购买信息 </a> </span>
				<div class="widget-buttons compact">
					<a href="" data-toggle="collapse"> <i class="fa fa-expand"></i> </a> 
				</div>
			</div>
			<div class="widget-body editor-body-sm editor-body-buyinfo">
				
			</div>
		</div>
		
		<!-- TODO 付款信息 -->
		<div class="widget radius-bordered mgbt-xs-10" id="editor-panel-payinfo">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> <a href="" data-toggle="collapse"> 付款信息 </a> </span>
				<div class="widget-buttons compact">
					<a href="" data-toggle="collapse"> <i class="fa fa-expand"></i> </a> 
				</div>
			</div>
			<div class="widget-body editor-body-sm editor-body-payinfo">
				
			</div>
		</div>
		
		<!-- TODO 购买历史 -->
		<div class="widget radius-bordered mgbt-xs-10" id="editor-panel-history">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> <a href="" data-toggle="collapse"> 购买历史 </a> </span>
				<div class="widget-buttons compact">
					<a href="" data-toggle="collapse"> <i class="fa fa-expand"></i> </a> 
				</div>
			</div>
			<div class="widget-body editor-body-sm">
				<table data-ajax="handlerSearch" 
							 data-method="post"
							 data-id-field="id" 
							 data-unique-id="id" 
							 data-pagination="true"  
							 data-check-on-init="true" 
							 data-side-pagination="server" 
							 data-mobile-responsive="true"
							 data-form-search="#search-form-history" id="table-history">
					<thead>
						<tr>
							<th data-checkbox="true"></th>
							<th data-field="student" 		data-align="center">宝宝姓名</th>
							<th data-field="item" 			data-align="center">购买项目</th>
							<th data-field="type" 			data-align="center">项目类型</th>
							<th data-field="createDate" data-align="center">购买时间</th>
							<th data-field="fromDate" 	data-align="center">起止时间</th>
							<th data-field="totals" 		data-align="center">总金额</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
		
	</div>
</div>

<!-- 提交表单 ========================================================= -->
<form id="editor-form-refund" action="/miwawa/{dbname}/contract/refund" method="post" 
			name="com.app.miwawa.api.esm.entity.contract.Refund" data-custom-action="true">
			<input type="hidden" name="status" value="0">
			<input type="hidden" name="invalid" value="F">
			<input type="hidden" name="studentId">
			<input type="hidden" name="contractId">
			<input type="hidden" name="customerId">
</form>

<script type="text/javascript">
<!--
	require(['domReady'],function(domReady){
		domReady(function () {
			jQuery("#search-form-buyinfo").data("template-id","#view-buyinfo-template");
			jQuery("#search-form-payinfo").data("template-id","#view-payinfo-template");
	
			initData();
		});
	});
	
	var contract;
	function initData(){
		contract = jQuery("#table-contract").data("selectedRow");
		
		loaderBuyinfo();
		
		jQuery(".editor-body-contract").loadform("load", {
			templateId: "#view-contract-template",
			templatePath: "/miwawa/content/esm/contract/template.html",
			templateData: contract
		});
		
		jQuery("#search-form-buyinfo").loadform("many",helperBuyinfo());
		
	 	jQuery("#search-form-payinfo").loadform("many");
	 	
		jQuery("#table-history").bootstrapTable({
			onLoadSuccess:function(data){
				require(['jquery.plugin.form'],function(form){
					form.chosen("#reason").select("#reason");
					form.chosen("#payment").select("#payment");
					form.datepicker(".date-selector");
					form.tooltip("[data-toggle='tooltip']");
				});
			}
		});
	}
	
	// 加载合同购买信息
	function loaderBuyinfo(){
		jQuery("#userid",".form-editor-refund").val(userid);
		jQuery("#username",".form-editor-refund").val(username);
		jQuery("#buying",".form-editor-refund").val(contract.buying);
		jQuery("#useing",".form-editor-refund").val(contract.useing);
		jQuery("#rested",".form-editor-refund").val(contract.rested);
		jQuery("#totalsPrice",".form-editor-refund").val(contract.totalsPrice);
		jQuery("#useingPrice",".form-editor-refund").val(contract.useingPrice);
		jQuery("#restedPrice",".form-editor-refund").val(contract.restedPrice);
		
		jQuery(":hidden[name='contractId'],[data-foreign-id]").val(contract.id);
		jQuery(":hidden[name='customerId']").val(contract.customerid);
		jQuery(":hidden[name='studentId']").val(contract.studentid);
		jQuery(":hidden[name='studentid']").val(contract.studentid);
	} 
	
	// 提交前回调
	function handlerBefore(){
		var status = jQuery(":radio[name='type']:checked",".form-editor-refund").val();
		jQuery(":hidden[name='status']","#editor-form-refund").val(status == "退费"?"refund":"finish");
		if(jQuery('input:radio[name="type"]:checked').val() == "终止"){
			jQuery.ajaxsend({
				url:"/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
				data:{
		       		"entityClass" : "com.app.miwawa.api.esm.entity.view.ContractView",
		          "and[0][operator]": "EQ",
		          "and[0][fieldName]": "studentid",					
		          "and[0][fieldValue]": contract.studentid,						
		          "and[1][operator]": "EQ",
		          "and[1][fieldName]": "item",					
		          "and[1][fieldValue]": contract.item,
		          "and[2][operator]": "EQ",
		          "and[2][fieldName]": "status",			
		          "and[2][fieldValue]": "close",
		          "order[0][sort]": "createDate",
		          "order[0][order]":"asc"
		  	},
		  	success:function(data) {
		  		var closeCon = data.rows;
		  		//有未启用的合同才执行
		  		if(closeCon.length > 0){
						jQuery.ajaxsend({
							url:"/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
							data:{
					       	"entityClass" : "com.app.miwawa.api.esm.entity.contract.Contract",
					       	"bean":{"id":closeCon[0].id,"status":"0"}
					  	},
					  	success:function(data){
					  		jQuery.ajaxsend({
									url : "/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
									data : {
							       		"entityClass" : "com.app.miwawa.api.esm.entity.view.ContractView",
							          "and[0][operator]": "EQ",
							          "and[0][fieldName]": "studentid",					
							          "and[0][fieldValue]": contract.studentid,						
							          "and[1][operator]": "EQ",
							          "and[1][fieldName]": "item",					
							          "and[1][fieldValue]": contract.item,						
							          "and[2][operator]": "EQ",
							          "and[2][fieldName]": "status",			
							          "and[2][fieldValue]": "finish"
							  	},
							  	success : function(data) {
							  		//得到该学生的终止合同
							  		var finishCon = data.rows;
										for(var i=0;i<finishCon.length;i++){
											jQuery.ajaxsend({
												url : "/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
												data : {
										       		"entityClass" : "com.app.miwawa.api.eam.entity.StudentLeaveLimit",
										          "and[0][operator]": "EQ",
										          "and[0][fieldName]": "contractId",					
										          "and[0][fieldValue]": finishCon[i].id				
										  	},
										  	success : function(data) {
										  		//拿到终止合同的请假数据并更新为未启用合同的id
										  		var leave = data.rows;
													for(var j=0;j<leave.length;j++){
														jQuery.ajaxsend({
															url : "/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
															data : {
													       		"entityClass" : "com.app.miwawa.api.eam.entity.StudentLeaveLimit",
													       		"bean":{"id":leave[j].id,"contractId":closeCon[0].id,"topLimit":"-1"}
													  	},
													  	success : function(data) {
													  		jQuery("#table-contract").bootstrapTable("refresh");
													  	}
														});
													}
										  	}
											});
										}
							  	}
								});
					  	}
						});
		  		}
		  	}
			});
		}
		return true;
	}
	
	// 保存退费信息后回调
	function handlerCallback(options){
		jQuery('[data-toggle="dispose"]')[0].click();
	}
//-->
</script>